package com.ylr.config.framework.controller.api;

import com.ylr.config.framework.service.EnumService;
import com.ylr.base.common.dto.NameValue;
import com.ylr.base.common.tool.Result;
import com.ylr.config.common.dto.enum_.EnumDto;
import com.ylr.config.common.enums.Enum;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wyw
 * date: 2023-05-02 18:59:16
 * className: EnumsController
 * version: 1.0
 * description:
 */
@Validated
@RestController
@RequestMapping("/api/enum")
@AllArgsConstructor
public class EnumApiController {

    private static final Logger log = LoggerFactory.getLogger(EnumApiController.class);

    private EnumService enumsService;

    /**
     * 获取枚举配置列表接口
     * 根据枚举类型，获取关联的所有枚举配置
     * @param type 枚举类型
     * @return 枚举配置列表
     */
    @GetMapping("/enums")
    public Result<List<NameValue<String>>> getEnumList(@RequestParam("type") Integer type) {
        log.info("api => 开始获取枚举配置列表操作。type={}", type);
        if (Enum.NULL.equals(Enum.getEnum(type))) {
            return Result.fail("枚举类型不存在，请确认");
        }

        List<NameValue<String>> list = enumsService.getEnumList(type);

        log.info("api => 获取枚举配置列表操作结束。list={}", list);
        return Result.success(list);
    }

    /**
     * 获取枚举值列表接口
     * @param type 枚举类型
     * @return 枚举值列表
     */
    @GetMapping("/value/list")
    public Result<List<Long>> getValueList(@RequestParam("type") Integer type) {
        log.info("api => 开始获取枚举值列表操作。type={}", type);

        List<Long> list = enumsService.getValueList(type);

        log.info("api => 获取枚举值列表操作结束。list={}", list);
        return Result.success();
    }

    /**
     * 获取枚举配置接口
     * @param id 主键
     * @return 枚举配置
     */
    @RequestMapping("/get")
    public Result<EnumDto> getEnum(@RequestParam("id") Long id) {
        log.info("api => 开始获取枚举配置操作。id={}", id);

        EnumDto enumDto = enumsService.getEnum(id);

        log.info("api => 获取枚举配置操作结束。{}", enumDto);
        return Result.success(enumDto);
    }

}
